﻿Public Class frmBuscaminas

    Public ee As System.Windows.Forms.MouseEventArgs

    Public MiJuegoBuscaminas As New Buscaminas
    Private tablero(14, 14) As String
    Private NumeroBotonNumeroMina As New List(Of Integer)
    Private RatonDerecho As New List(Of Integer)
    Private Tiempo As Integer
    Private PosiblesMinasRestantes As Integer = MiJuegoBuscaminas._NumeroDeMinas
    Private Puntuacion As Integer
    Private puntuaciontotal As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ContenedorDeMinas.Enabled = True
        RatonDerecho.Clear()
        lbltiempo.Text = Tiempo
        lblPosiblesMinasRestantes.Text = MiJuegoBuscaminas._NumeroDeMinas
        NumeroBotonNumeroMina.Clear()
        tablero = MiJuegoBuscaminas.generarMinas()
        For i = 0 To ContenedorDeMinas.Controls.Count - 1
            ContenedorDeMinas.Controls(i).BackgroundImage = ImageList1.Images("BuscaminasNormal.png")
            ContenedorDeMinas.Controls(i).Enabled = True
        Next
        Dim horizontal As Integer = 0
        Dim vertical As Integer = 0
        For i = 0 To 224
            ContenedorDeMinas.Controls(i).Tag = i
            If horizontal > 14 Then
                horizontal = 0
                vertical += 1
            End If
            ContenedorDeMinas.Controls(i).Text = tablero(horizontal, vertical)
            horizontal += 1
        Next
        For i = 0 To ContenedorDeMinas.Controls.Count - 1
            NumeroBotonNumeroMina.Add(ContenedorDeMinas.Controls(i).Text)
        Next
        For i = 0 To ContenedorDeMinas.Controls.Count - 1
            ContenedorDeMinas.Controls(i).Text = ""
        Next
    End Sub

    Private Sub btnReiniciar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReiniciar.Click

        ContenedorDeMinas.Enabled = True
        NumeroBotonNumeroMina.Clear()
        tablero = MiJuegoBuscaminas.generarMinas()
        For i = 0 To ContenedorDeMinas.Controls.Count - 1
            ContenedorDeMinas.Controls(i).BackgroundImage = ImageList1.Images("BuscaminasNormal.png")
            ContenedorDeMinas.Controls(i).Enabled = True
        Next
        Dim horizontal As Integer = 0
        Dim vertical As Integer = 0
        For I = 0 To 224
            If horizontal > 14 Then
                horizontal = 0
                vertical += 1
            End If
            ContenedorDeMinas.Controls(I).Text = tablero(horizontal, vertical)
            horizontal += 1
        Next
        For i = 0 To ContenedorDeMinas.Controls.Count - 1
            NumeroBotonNumeroMina.Add(ContenedorDeMinas.Controls(i).Text)
        Next
        For i = 0 To ContenedorDeMinas.Controls.Count - 1
            ContenedorDeMinas.Controls(i).Text = ""
        Next
        RatonDerecho.Clear()
        Tiempo = 0
        lbltiempo.Text = Tiempo
        Puntuacion = 0
        lblPuntos.Text = Puntuacion
        PosiblesMinasRestantes = MiJuegoBuscaminas._NumeroDeMinas
        lblPosiblesMinasRestantes.Text = PosiblesMinasRestantes
    End Sub

    Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button112.MouseDown, Button225.MouseDown, Button1.MouseDown, Button2.MouseDown, Button3.MouseDown, Button4.MouseDown, Button5.MouseDown, Button6.MouseDown, Button7.MouseDown, Button8.MouseDown, Button9.MouseDown, Button10.MouseDown, Button11.MouseDown, Button13.MouseDown, Button14.MouseDown, Button15.MouseDown, Button16.MouseDown, Button17.MouseDown, Button18.MouseDown, Button19.MouseDown, Button20.MouseDown, Button21.MouseDown, Button22.MouseDown, Button23.MouseDown, Button24.MouseDown, Button25.MouseDown, Button26.MouseDown, Button27.MouseDown, Button28.MouseDown, Button29.MouseDown, Button30.MouseDown, Button31.MouseDown, Button32.MouseDown, Button33.MouseDown, Button34.MouseDown, Button35.MouseDown, Button36.MouseDown, Button37.MouseDown, Button38.MouseDown, Button39.MouseDown, Button40.MouseDown, Button41.MouseDown, Button42.MouseDown, Button43.MouseDown, Button44.MouseDown, Button45.MouseDown, Button46.MouseDown, Button47.MouseDown, Button48.MouseDown, Button49.MouseDown, Button50.MouseDown, Button51.MouseDown, Button52.MouseDown, Button53.MouseDown, Button54.MouseDown, Button55.MouseDown, Button56.MouseDown, Button57.MouseDown, Button58.MouseDown, Button59.MouseDown, Button60.MouseDown, Button61.MouseDown, Button62.MouseDown, Button63.MouseDown, Button64.MouseDown, Button65.MouseDown, Button66.MouseDown, Button67.MouseDown, Button68.MouseDown, Button69.MouseDown, Button70.MouseDown, Button71.MouseDown, Button72.MouseDown, Button73.MouseDown, Button74.MouseDown, Button75.MouseDown, Button76.MouseDown, Button77.MouseDown, Button78.MouseDown, Button79.MouseDown, Button80.MouseDown, Button81.MouseDown, Button82.MouseDown, Button83.MouseDown, Button84.MouseDown, Button85.MouseDown, Button86.MouseDown, Button87.MouseDown, Button88.MouseDown, Button89.MouseDown, Button90.MouseDown, Button91.MouseDown, Button92.MouseDown, Button93.MouseDown, Button94.MouseDown, Button95.MouseDown, Button96.MouseDown, Button97.MouseDown, Button98.MouseDown, Button99.MouseDown, Button100.MouseDown, Button101.MouseDown, Button102.MouseDown, Button103.MouseDown, Button104.MouseDown, Button105.MouseDown, Button106.MouseDown, Button107.MouseDown, Button108.MouseDown, Button109.MouseDown, Button110.MouseDown, Button111.MouseDown, Button12.MouseDown, Button113.MouseDown, Button114.MouseDown, Button115.MouseDown, Button116.MouseDown, Button117.MouseDown, Button118.MouseDown, Button119.MouseDown, Button120.MouseDown, Button121.MouseDown, Button122.MouseDown, Button123.MouseDown, Button124.MouseDown, Button125.MouseDown, Button126.MouseDown, Button127.MouseDown, Button128.MouseDown, Button129.MouseDown, Button130.MouseDown, Button131.MouseDown, Button132.MouseDown, Button133.MouseDown, Button134.MouseDown, Button135.MouseDown, Button136.MouseDown, Button137.MouseDown, Button138.MouseDown, Button139.MouseDown, Button140.MouseDown, Button141.MouseDown, Button142.MouseDown, Button143.MouseDown, Button144.MouseDown, Button145.MouseDown, Button146.MouseDown, Button147.MouseDown, Button148.MouseDown, Button149.MouseDown, Button150.MouseDown, Button151.MouseDown, Button152.MouseDown, Button153.MouseDown, Button154.MouseDown, Button155.MouseDown, Button156.MouseDown, Button157.MouseDown, Button158.MouseDown, Button159.MouseDown, Button160.MouseDown, Button161.MouseDown, Button162.MouseDown, Button163.MouseDown, Button164.MouseDown, Button165.MouseDown, Button166.MouseDown, Button167.MouseDown, Button168.MouseDown, Button169.MouseDown, Button170.MouseDown, Button171.MouseDown, Button172.MouseDown, Button173.MouseDown, Button174.MouseDown, Button175.MouseDown, Button176.MouseDown, Button177.MouseDown, Button178.MouseDown, Button179.MouseDown, Button180.MouseDown, Button181.MouseDown, Button182.MouseDown, Button183.MouseDown, Button184.MouseDown, Button185.MouseDown, Button186.MouseDown, Button187.MouseDown, Button188.MouseDown, Button189.MouseDown, Button190.MouseDown, Button191.MouseDown, Button192.MouseDown, Button193.MouseDown, Button194.MouseDown, Button195.MouseDown, Button196.MouseDown, Button197.MouseDown, Button198.MouseDown, Button199.MouseDown, Button200.MouseDown, Button201.MouseDown, Button202.MouseDown, Button203.MouseDown, Button204.MouseDown, Button205.MouseDown, Button206.MouseDown, Button207.MouseDown, Button208.MouseDown, Button209.MouseDown, Button210.MouseDown, Button211.MouseDown, Button212.MouseDown, Button213.MouseDown, Button214.MouseDown, Button215.MouseDown, Button216.MouseDown, Button217.MouseDown, Button218.MouseDown, Button219.MouseDown, Button220.MouseDown, Button221.MouseDown, Button222.MouseDown, Button223.MouseDown, Button224.MouseDown

        Dim b As Button = DirectCast(sender, Button)
        If Tiempo = 0 Then
            Timer1.Start()
        End If
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If RatonDerecho.Contains(b.Tag) Then
                Exit Sub
            End If
            If NumeroBotonNumeroMina(b.Tag) = 9 Then
                ContenedorDeMinas.Enabled = False

                Timer1.Stop()

                For a = 0 To MiJuegoBuscaminas.PosicionamientoDeMinas.Count - 1
                    ContenedorDeMinas.Controls(MiJuegoBuscaminas.PosicionamientoDeMinas(a)).BackgroundImage = ImageList1.Images("BuscaminasMina.png")
                Next
                Puntuacion = Puntuacion - 20
                lblPuntos.Text = Puntuacion
                puntuaciontotal = lblPuntos.Text
                MiJuego.UsuarioActual.SumarPuntos(0, puntuaciontotal)
                Exit Sub
            Else
                SELECCIONARCASO(b.Tag)
                Dim NumeroAciertos As Integer = 0
                For i = 0 To ContenedorDeMinas.Controls.Count - 1
                    If ContenedorDeMinas.Controls(i).Enabled = True Then
                        NumeroAciertos += 1
                    End If
                Next
                If NumeroAciertos = MiJuegoBuscaminas._NumeroDeMinas Then
                    MessageBox.Show("Terminaste")
                    Puntuacion = Puntuacion + 50
                    puntuaciontotal = lblPuntos.Text
                    MiJuego.UsuarioActual.SumarPuntos(0, puntuaciontotal)
                    Timer1.Stop()
                    ContenedorDeMinas.Enabled = False
                End If
            End If
        End If
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If RatonDerecho.Contains(b.Tag) Then
                ContenedorDeMinas.Controls(b.Tag).BackgroundImage = ImageList1.Images("BuscaminasNormal.png")
                RatonDerecho.Remove(b.Tag)
                PosiblesMinasRestantes += 1
                lblPosiblesMinasRestantes.Text = PosiblesMinasRestantes
            Else
                ContenedorDeMinas.Controls(b.Tag).BackgroundImage = ImageList1.Images("BuscaminasCuidado.png")
                RatonDerecho.Add(b.Tag)
                PosiblesMinasRestantes -= 1
                lblPosiblesMinasRestantes.Text = PosiblesMinasRestantes
            End If
        End If
        lblPuntos.Text = Puntuacion
    End Sub

    Private Sub SELECCIONARCASO(ByVal numero As Integer)
        Dim PuntosSumar As Integer = 0
        If NumeroBotonNumeroMina(numero) = 0 Then
            Dim test As New List(Of Integer)
            test = MiJuegoBuscaminas.ComprobarAlrededor(numero, NumeroBotonNumeroMina)
            PuntosSumar = test.Count
            Puntuacion = Puntuacion + (PuntosSumar * 5)
            For i = 0 To test.Count - 1
                ContenedorDeMinas.Controls(test(i)).BackgroundImage = ImageList1.Images(NumeroBotonNumeroMina(test(i)))
                ContenedorDeMinas.Controls(test(i)).Enabled = False
            Next
        Else
            Puntuacion = Puntuacion + 5
            ContenedorDeMinas.Controls(numero).BackgroundImage = ImageList1.Images(NumeroBotonNumeroMina(numero))
            ContenedorDeMinas.Controls(numero).Enabled = False
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Tiempo += 1
        lbltiempo.Text = Tiempo
    End Sub

End Class